
// pages/person/person.js
const db = wx.cloud.database();
 
/**
 
 */
Page({

    /**
     * 页面的初始数据
     */
    data: {

        userInfo: {},
        hasUserInfo: false,
        canIUseGetUserProfile: false,
        // success代表已经是接单员了, 
        // fail代表曾经申请过但是没通过
        // loading代表目前有正在审核中的
        // null代表从未申请过
        personReceiveState: '',
        admin: false,
        a: "",
    },

    orderReceiver() {
        wx.navigateTo({
            url: '/pages/orderReceiver/orderReceiver',
        })
    },

    // 接单申请判断
    applyOrder() {
        const {
            personReceiveState
        } = this.data;
        if (personReceiveState === 'success') {
            wx.showModal({
                title: '提示',
                content: '您已经是接单员了, 请勿重复申请!',
                showCancel: false
            })
        } else if (personReceiveState === 'fail') {
            wx.showModal({
                title: '提示',
                content: '您之前提交的申请未通过审核, 您可以继续申请, 如有疑问请联系管理员: YXHLYF19115314436',
                success: (res) => {
                    if (res.confirm) {
                        wx.setClipboardData({
                            data: 'YXHLYF19115314436',
                            success: function (res) {
                                wx.showToast({
                                  title: '微信号已复制',
                                  //icon:"none",
                                  image:"/imgs/提交成功.png", //只能是本地的
                                  mask:"true" //是否设置点击蒙版，防止点击穿透
                                })
                             }
                        })
                        wx.navigateTo({
                            url:  '/pages/applyOrder/applyOrder',
                        })
                    } else if (res.cancel) {
                        wx.navigateTo({
                            url:  '/pages/applyOrder/applyOrder',
                        })
                    }
                }
            })
        } else if (personReceiveState === 'loading') {
            wx.showModal({
                title: '提示',
                content: '您之前申请的内容正在审核中, 请耐心等待! 如加急审核请添加管理员微信: YXHLYF19115314436',
                //showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.setClipboardData({
                            data: 'YXHLYF19115314436',
                            success: function (res) {
                                wx.showToast({  
                                  title: '微信号已复制',
                                  //icon:"none",
                                  image:"/imgs/提交成功.png", //只能是本地的
                                  mask:"true"//是否设置点击蒙版，防止点击穿透
                                })
                          }
                        })
                    }
                }
            }) 
    
        } else if (personReceiveState === 'null') {
            wx.navigateTo({
              url: '/pages/applyOrder/applyOrder',
            })
        }
    },

  // 判断当前用户是否是管理员
  getAdminPower() {
    wx.cloud.callFunction({
    name: 'getopenID',
    success: (res) => {
      console.log("为",res.result.openid)
      db.collection('admin').where({
        adminID: res.result.openid
      }).get({
        success: (res) => {
            console.log("1",res)
            this.setData({
                admin: !!res.data.length
               })
             }
          })
        }
    })
},
    /**
     * 生命周期函数--监听页面加载
     */
onLoad: function (options) {
        // 过审核
        const that = this
/*         db.collection("a").get()
          .then(res => {
              console.log("11111111",res.data[0].a)
            that.setData({
              a: res.data[0].a,
            })
          }) */
    
    if (wx.getUserProfile) {
        this.setData({
            canIUseGetUserProfile: true
        })
    }
    wx.showLoading({
        title: '加载中',
    })
    const userInfo = wx.getStorageSync('userInfo');
    /* console.log(userInfo) */
    this.setData({
        hasUserInfo: !!userInfo,
        userInfo: userInfo,
    })
    let personReceiveState = '';
    this.getAdminPower();
    db.collection('orderReceive').where({
        _openid: wx.getStorageSync('openid')
    }).get({
        success: (res) => {
            const {
                data
            } = res;
            if (data.length) {
                for (let i = 0; i < data.length; i++) {
                    if (data[i].state === '通过') {
                        personReceiveState = 'success';
                        break;
                    } else if (data[i].state === '不通过') {
                        personReceiveState = 'fail';
                    } else {
                        personReceiveState = 'loading';
                        break;
                    }
                }
            } else {
                personReceiveState = 'null';
            }
            this.setData({
                personReceiveState,
            })
            wx.hideLoading();
        }
    })
},
 
    // 授权
/*     getUserProfile() {
        wx.getUserProfile({
          desc: '授权才能使用完整功能',
          success: res => {
            let user = res.userInfo
    
             
            try {
              wx.setStorageSync('key', user)
              wx.showToast({
                title: '登录成功！！！',
                image: "/imgs/登录成功.png",  
    
              })
            } catch (e) {
               
            }
    
            
            this.setData({
              userInfo: user,
            })
            console.log("用户信息", user)
          },
          fail: res => {
            console.log("授权失败", res)
          }
        })
      }, */


      getUserProfile() {
        wx.getUserProfile({
            desc: '获取用户信息',
            success: (res) => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
                wx.setStorageSync('userInfo', res.userInfo);
            }
        })
    },

    // 老接口
    getUserInfo(e) {
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },


 
   
})